package com.think.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

public class WebUtils {
	public static void outputJsonFile(File jsonFile, HttpServletRequest request, HttpServletResponse response) {
	     String contentType = "text/html;charset=UTF-8";
	     response.setContentType(contentType);
	     response.setHeader("Cache-Control", "no-store, no-cache");
	     response.setHeader("Pragma", "no-cache");
	     InputStream in = null;
	     OutputStream out = null;
	     try {
	         in = new FileInputStream(jsonFile);
	         out = response.getOutputStream();
	         IOUtils.copy(in, out);
	         out.flush();
	     } catch (Exception ex) {
	         throw new RuntimeException(ex);
	     } finally {
	         IOUtils.closeQuietly(in);
	         IOUtils.closeQuietly(out);
	     }
	 }
	
	public static void outputJson(Object object, HttpServletRequest request, HttpServletResponse response) {
	     String contentType = "application/json;charset=UTF-8";//"text/html;charset=UTF-8";
	     response.setContentType(contentType);
	     response.setHeader("Cache-Control", "no-store, no-cache");
	     response.setHeader("Pragma", "no-cache");
	     Writer writer = null;
	     try {
	         writer = response.getWriter();
	         JSonUtil.toJson(object, writer);
	         writer.flush();
	     } catch (Exception ex) {
	         throw new RuntimeException(ex);
	     } finally {
	         IOUtils.closeQuietly(writer);
	     }
	 }
}
